<?php
/**
 * Created by PhpStorm.
 * User: wang
 * Date: 2016/7/29
 * Time: 14:58
 */
namespace Home\Controller;

use Org\Util\Date;
use Think\Controller\RestController;

class ParkController extends RestController
{

//    function _initialize()
//    {
//        if (!session('user_id')) {
//            $this->ajaxReturn(array(
//                'status' => 'error',
//                'info' => '未登陆'
//            ));
//        }
//    }

    public function index_get($id=0)
    {
        $res = M('parking')->where('parking_id=' . $id)->find();
        if ($res) {
            $this->ajaxReturn($res);
        } else {
            $this->ajaxReturn(array('status' => 'error', 'info' => '该车位不可用'));
        }
    }

    public function index_post($id = 0)
    {
        $data = array(
            'user_id' => I('post.user_id',session('user_id')),
            'start_time' => I('post.start_time'),
            'end_time' => I('post.end_time'),
            'Lng' => I('post.Lng'),
            'Lat' => I('post.Lat'),
            'lock_no'=>I('post.lock_no'),
            'lock_bind'=>I('post.lock_bind'),
            'status'=>I('post.status'),
            'location'=>I('post.location'),
            'charge' => I('post.charge')
        );
        if ($id) {
            $res = M('parking')->where('parking_id=' . $id)->save($data);
        } else {
            $data['status']=C('CAN_BE_USE');
            $res = M('parking')->add($data);
        }

        if ($res) {
            $this->ajaxReturn($data);
        } else {
            $this->ajaxReturn(array('status' => 'error', 'info' => '错误'));
        }
    }

    public function index_delete($id=0)
    {
        $res = M('parking')->where('parking_id=' . $id)->setField('status', C('DELETED_PARK'));
        if ($res) {
            $this->ajaxReturn($res);
        } else {
            $this->ajaxReturn(array('status' => 'error', 'info' => '删除失败'));
        }
    }

    public function listPark($lng, $lat, $range = 1)
    {
        //0.009°=1km
        $maxLng = $lng + abs(0.009 * $range / cos($lng / 180 * M_PI));
        $minLng = $lng - abs(0.009 * $range / cos($lng / 180 * M_PI));
        $maxLat = $lat + (0.009 * $range);
        $minLat = $lat - (0.009 * $range);
        $res = M('parking')->where(array(
            'Lng' => array(array('gt', $minLng), array('lt', $maxLng), 'and'),
            'Lat' => array(array('gt', $minLat), array('lt', $maxLat), 'and'),
            'status' => array('gt', 0)
        ))->select();//todo
        if ($res) {
            $this->ajaxReturn($res);
        } else {
            $this->ajaxReturn(array(
                'status' => 'error',
                'info' => '当前范围内没有可用车位'
            ));
        }
    }

    public function start($id=0)
    {
        $park = M('parking')->where('parking_id=' . $id)->find();
        if ($park['status'] > 0) {
            M('parking')->where('parking_id=' . $id)->setField('status', C('IN_USE_PARK'));
            M('using')->add(array(
                'user_id' => session('user_id'),
                'parking_id' => $id,
                'start_time' => new Date() . ''
            ));
            $this->ajaxReturn(array(
                'status' => 'success'
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 'error',
                'info' => '该车位不可用'
            ));
        }
    }

    public function end($id=0)
    {
        $where = array(
            'parking_id' => $id,
            'user_id' => session('user_id')
        );
        $using = M('using')->where($where)->find();
        if ($using) {
            $d = new Date();
            $date = $d . '';
            $user = M('user');
            $income = M('income');
            $history = M('history');
            $parking = M('parking');

            $park = $parking->where('parking_id=' . $using['parking_id'])->find();
            $owner_id = $park['user_id'];
            $total = $d->dateDiff($using['start_time'], 'm');
            $cost = round($total) * $park['charge']/-60;
            $income->add(array(
                'user_id' => $owner_id,
                'parking_id' => $using['parking_id'],
                'income' => $cost,
                'date' => $date
            ));
            $user->where('id=' . session('user_id'))->setDec('balance', $cost);
            $user->where('id=' . $owner_id)->setInc('balance', $cost);
            $history->add(array(
                'id' => $using['id'],
                'user_id' => $using['user_id'],
                'parking_id' => $id,
                'start_time' => $using['start_time'],
                'end_time' => $date,
                'cost' => $cost
            ));
            $parking->where('parking_id=' . $using['parking_id'])->setField('status', 1);
            M('using')->where($where)->delete();
            $this->ajaxReturn(array(
                'status' => 'success'
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 'error'
            ));
        }
//        var_dump($using);
    }

}